package com.glory.dmegcpda.view

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.view.View
import com.glory.dmegcpda.R
import com.glory.dmegcpda.base.Constant
import com.glory.dmegcpda.viewmodel.SystemSetModel
import com.glory.mvvmnet.base.BaseActivity
import com.glory.mvvmnet.entity.ToastMessageBean
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_SharePreferencesUtils
import kotlinx.android.synthetic.main.activity_system_set.*

class SystemSetActivity : BaseActivity<SystemSetModel>(), View.OnClickListener {

    override fun layoutId(): Int {
        return R.layout.activity_system_set
    }

    override fun initView(savedInstanceState: Bundle?) {
        tvServerText.setText(Constant.mUrl)
        rlServerAddress.setOnClickListener(this)

        rlFileAddress.setOnClickListener(this)
        ivBack.setOnClickListener(this)
    }

    override fun initData() {

    }

    @SuppressLint("SetTextI18n")
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        when (resultCode) {
            1 -> {
                val extras = data!!.getExtras()
                val string = extras!!.getString("server_address")
                tvServerText.setText(string)
                FreeApi_SharePreferencesUtils.setSharePre("SystemSet", "sercerUrl", string, applicationContext)
                Constant.mUrl = string!!
                Constant.mainUrl = Constant.http + Constant.mUrl + Constant.url
                viewModel.defUI.toastEvent.value = ToastMessageBean("保存成功",true)
            }
            2 -> {
                val extras = data!!.getExtras()
                val string = extras!!.getString("file_address")
                tvFileText.setText(string)
                viewModel.defUI.toastEvent.value = ToastMessageBean("保存成功", true)
            }
        }
    }

    override fun onClick(view: View?) {
        when (view) {
            rlServerAddress -> {
                val intent = Intent()
                intent.putExtra("type", "server_address")
                intent.setClass(this, SkipDialogActivity::class.java)
                startActivityForResult(intent, 1)
            }
            rlFileAddress -> {
                val intent = Intent()
                intent.putExtra("type", "file_address")
                intent.setClass(this, SkipDialogActivity::class.java)
                startActivityForResult(intent, 1)
            }
            ivBack -> {
                finish()
            }
        }
    }

}